﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BioBooking.Data;
using System.ComponentModel;

namespace BioBooking.Client.Screens
{
    /// <summary>
    /// Interaction logic for BookingUpdate.xaml
    /// </summary>
    public partial class BookingUpdate : UserControl
    {
        IRepository repository;
        ObservableCollection<Member> members;

        ICollectionView collectionview;
        
        public BookingUpdate()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
	        {
		        repository = Repository.GetRepository();
                members = new ObservableCollection<Member>(repository.ListAllMembers());

                collectionview = CollectionViewSource.GetDefaultView(members);
                collectionview.Filter = new Predicate<object>(FilterCollectionQuery);

                lbCustomers.DataContext = members;
	        }
        }

        public bool FilterCollectionQuery(object item)
        {
            Member member = item as Member;

            if (member == null)
                return false;

            if (tbxTlfNr.Text.Trim().Length == 0 && tbxCustomerName.Text.Trim().Length == 0)
                return true;

            bool tlfNrFound = member.Telephone.ToLower().Contains(tbxTlfNr.Text.ToLower()) 
                && tbxTlfNr.Text.Trim().Length > 0;

            bool nameFound = member.FullName.ToLower().Contains(tbxCustomerName.Text.ToLower()) 
                && tbxCustomerName.Text.Trim().Length > 0;

            if (tbxCustomerName.Text.Trim().Length > 0 & tbxTlfNr.Text.Trim().Length > 0)
            {
                return tlfNrFound && nameFound;
            }
            else if (tbxCustomerName.Text.Trim().Length > 0)
            {
                return nameFound;
            }
            else if (tbxTlfNr.Text.Trim().Length > 0)
            {
                return tlfNrFound;
            }

            return false;
        }

        private void tbxCustomerName_TextChanged(object sender, TextChangedEventArgs e)
        {
            collectionview.Refresh();
        }

        private void tbxTlfNr_TextChanged(object sender, TextChangedEventArgs e)
        {
            collectionview.Refresh();
        }

        private void lbCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Member member = lbCustomers.SelectedValue as Member;

            if (member == null)
            {
                return;
            }

            lbBookings.DataContext = member.Booking;
        }

        private void lbBookings_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Booking booking = lbBookings.SelectedValue as Booking;

            if (booking == null)
            {
                return;
            }

            int count = booking.Seats.Count;
            decimal price = booking.Showing.UnitPrice;

            tbNrOfTickets.Text = count.ToString();
            tbMovieTitle.Text = booking.Showing.Movie.Titel;
            tbStartTime.Text = booking.Showing.StartTime.ToString();
            tbFullPrice.Text = (Convert.ToInt32(price) * count).ToString();
        }

        private void btnDeleteBooking_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

